package br.com.fatecpg.fatecMobile;
 
import java.util.ArrayList;
import java.util.HashMap;
 
import br.com.fatecpg.sharepoint.SharepointList;
import br.com.fatecpg.utils.ApplicationUtils;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
 
public class FaultsActivity extends Activity {
        ListView faultsListView = null;
        HashMap<String, ArrayList<String>> faultsMap = new HashMap<String, ArrayList<String>>();
 
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_faults);
 
                try {
                        ArrayList<String> attributes = new ArrayList<String>();
                        attributes.add("ows_LinkTitle");
                        attributes.add("ows_Disciplina");
                        attributes.add("ows_Turno");
                        attributes.add("ows_Data");
                        attributes.add("ows_Semestre");
                        attributes.add("ows_Observa_x00e7__x00e3_o");
                        attributes.add("ows_Created_x0020_Date");
                        faultsMap = SharepointList.getList("Faltas","10","",attributes);
                        ArrayList<String> list = faultsMap.get("ows_Disciplina");
                        for(int i = 0; i < list.size();i++){
                        	list.set(i, ApplicationUtils.removeSharepointId(list.get(i)));
                        }
                        faultsListView = (ListView) findViewById(R.id.faltasListView);
                        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                                        android.R.layout.simple_list_item_1, list);
                        faultsListView.setAdapter(dataAdapter);
 
                        faultsListView.setOnItemClickListener(new OnItemClickListener() {
 
                                public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                                       
                                        //String modified = faultsMap.get("ows_Modified").get(position);
                                        //String expires = faultsMap.get("ows_Expires").get(position);
                                        String title = faultsMap.get("ows_LinkTitle").get(position);
                                        String created = ApplicationUtils.formatInverseDate(ApplicationUtils.removeSharepointId(faultsMap.get("ows_Created_x0020_Date").get(position)));
                                        String discipline = faultsMap.get("ows_Disciplina").get(position);
                                        String period = ApplicationUtils.removeSharepointId(faultsMap.get("ows_Turno").get(position)).replace(";", "");
                                        String date = ApplicationUtils.formatInverseDate(faultsMap.get("ows_Data").get(position));
                                        //date = date.substring("#");
                                        String semester = ApplicationUtils.removeSharepointId(faultsMap.get("ows_Semestre").get(position));
                                        String note = faultsMap.get("ows_Observa_x00e7__x00e3_o").get(position);
                                        AlertDialog.Builder detalhes = new AlertDialog.Builder(
                                                        FaultsActivity.this);
                                        detalhes.setTitle(title);
                                        detalhes.setMessage(ApplicationUtils.removeHtmlTags("Disciplina: " + discipline)
                                                        .trim()
                                                        + "\n\n"
                                                        + "Data: "
                                                        + date
                                                        + "\n"
                                                        + "Obs: "
                                                        + note
                                                        +"\n"
                                                        + "Semestre: "
                                                        + semester
                                                        +"\n"
                                                        + "Turno: "
                                                        + period
                                                        +"\n"
                                                        + "Criado em: "
                                                        +created
                                                        );
                                        detalhes.setNeutralButton("OK", null);
                                        detalhes.show();
 
                                }
 
                        });
                } catch (Exception ex) {
 
                }
        }
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.faults, menu);
                return true;
        }
 
}